<?php

	/**
   * add-program is reached from the manage-courses page, user completes fields
   * and submits to save program information to the database
   * 
   * @Author Tyler Bradovich
   * @Version 1.0
   */

    @session_start();
    require_once 'shared-functions.php';
    require_once 'session.php';
    require_once 'masterpage.php';
    
    if(!IsValidSession())
    {
        header('Location: login.php?page=add-program');
        exit();
    }
    else
    {
    	RefreshSession();
    }

	$user = GetCurrentUserAccessLevel();
	
    //Check user access
    if($user != $DIRECTOR && $user != $ADMIN && $user != $PRINCIPLE)
    {
        header('Location: login.php?page=add-program&error=To access to the add program page, please log in as a director, principle or admin&logout=1');
        exit();
    }
	
    masterpage("Manage Programs");
	
	
	echo "<a href='manage-programs.php'>&lt;&lt; Back to Manage Programs</a>";
	
	$Name = '';
	$Year = '';
	$Code = '';
	$Semester = '';
	$ApplicationFee = '';
	$InterviewFee = '';
	$TuitionFee = '';
	
	$validInput = true;
	$hideInput = false;
	
	if (isset($_POST['submitted'])) // Clicked submit
	{
		$Name = $_POST['Name'];
		$Year = $_POST['Year'];
		$Code = $_POST['Code'];
		$Semester = $_POST['Semester'];
		$ApplicationFee = $_POST['ApplicationFee'];
		$InterviewFee = $_POST['InterviewFee'];
		$TuitionFee = $_POST['TuitionFee'];
		
		// Validate Length
		if (strlen($Code) > 3)
		{
			echo "<p class='errorText'>Course Code cannot be more than 3 characters long.</p>";
			$validInput = false;
		}
		// Require all fields
		if ($_POST['Name'] == '' || $_POST['Year'] == '' || $_POST['Code'] == '' || $_POST['Semester'] == '' || $_POST['ApplicationFee'] == '' || $_POST['InterviewFee'] == '' || $_POST['TuitionFee'] == '')
		{
			echo "<p class='errorText'>All fields are required.</p>";
			$validInput = false;
		}
		else if (!is_numeric($_POST['Year']) || !is_numeric($_POST['Semester']) || !is_numeric($_POST['ApplicationFee']) || !is_numeric($_POST['InterviewFee']) || !is_numeric($_POST['TuitionFee']))
		{
			// Check numeric-only fields
			echo "<p class='errorText'>Make sure numeric fields (Year, Semester, Fees) only contain numbers.</p>";
			$validInput = false;
		}
		
		if ($validInput)
		{
			$link = connect_db();
			$query = "INSERT INTO `Program`
					(Name, Year, Code, Semester, ApplicationFee, InterviewFee, TuitionFee) VALUES
					('$_POST[Name]', '$_POST[Year]', '$_POST[Code]', '$_POST[Semester]',
					'$_POST[ApplicationFee]', '$_POST[InterviewFee]', '$_POST[TuitionFee]');";
			mysql_query($query, $link);
			if (mysql_affected_rows())
			{
				echo "<br/><br/><strong>Program added</strong>";
				
				echo "<br/><br/>";
				echo "Name: $Name<br/>";
				echo "Year: $Year<br/>";
				echo "Code: $Code<br/>";
				echo "Semester: $Semester<br/>";
				echo "Application Fee: $ApplicationFee<br/>";
				echo "Interview Fee: $InterviewFee<br/>";
				echo "Tuition Fee: $TuitionFee<br/>";
			}
			else
				echo "<br/><br/><strong>Program NOT added</strong>";
				
			$hideInput = true;
		}
	}
	
	if (!$hideInput)
	{	
		echo "<form action='add-program.php' method='POST'>";
		echo "	<p>Name:<span class='requiredText'>*</span><br/><input type='text' name='Name' value='$Name'/></p>";
		echo "	<p>Year:<span class='requiredText'>*</span><br/><input type='text' name='Year' value='$Year'/></p>";
		echo "	<p>Code:<span class='requiredText'>*</span><br/><input type='text' name='Code' value='$Code'/></p>";
		echo "	<p>Semester:<span class='requiredText'>*</span><br/><input type='text' name='Semester' value='$Semester'/></p>";
		/* 
		<select name='Semester'>";
        //generate semesters
        $query = "SELECT * FROM `Semester`";
        $link = connect_db();
        $results = mysql_query($query, $link);
        while($result = mysql_fetch_array($results, MYSQL_BOTH))
        {
            echo '<option value="'.$result['semesterID'].'">'.$result['Description'].'</option>';
        }
        echo "  </select>";
		*/
		echo "	<p>Application Fee:<span class='requiredText'>*</span><br/><input type='text' name='ApplicationFee' value='$ApplicationFee'/></p>";
		echo "	<p>Interview Fee:<span class='requiredText'>*</span><br/><input type='text' name='InterviewFee' value='$InterviewFee'/></p>";
		echo "	<p>School Fees:<span class='requiredText'>*</span><br/><input type='text' name='TuitionFee' value='$TuitionFee'/></p>";
		echo "	<input type='submit' value='Add Program'><input type='hidden' value='1' name='submitted'/>";
		echo "</form>";
	}
    
    endmasterpage();
?>